package com.socialin.android.hackaton.draw.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.socialin.android.hackaton.draw.R;

public class ChatListAdapter extends ArrayAdapter<JSONObject>{
	
	LayoutInflater inflater = null;
	
	private String htmlHeader = "<HTML><HEAD></HEAD><BODY>";
	private String htmlFooter = "</body>";
	

	public ChatListAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			convertView = inflater.inflate(R.layout.chat_list_item, null);
		}
		
		JSONObject item = null;
		JSONObject rootItem = getItem(position);
		try {
			item = rootItem.getJSONArray("params").getJSONObject(0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("json = "+rootItem.toString());
		
		TextView userNameTextView = (TextView) convertView.findViewById(R.id.chat_sender_username_textview);
		ImageView userAvatarImageView = (ImageView) convertView.findViewById(R.id.chat_sender_avatar_imageview);
		WebView chatContentWebView = (WebView) convertView.findViewById(R.id.chat_content_webview);
		
		
		userNameTextView.setText(rootItem.optString("user_name"));
		
		long dateMilis = rootItem.optLong("date");
		if(dateMilis == 0){
			dateMilis = System.currentTimeMillis();
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getDefault());
		calendar.setTimeInMillis(dateMilis);
		String time = calendar.getTime().toLocaleString();
			
		((TextView)convertView.findViewById(R.id.chat_message_date_textview)).setText(time);
			
		String innerHTML = htmlHeader+"<p style=\"color:black\">"+changeTextToEmotion(item.optString("message").replace("\n", "<br/>"))+"</p>"+htmlFooter;
		chatContentWebView.loadDataWithBaseURL("file:///android_asset/", innerHTML, "text/html", "utf-8", null);
		chatContentWebView.getSettings().setJavaScriptEnabled(true);
		
		return convertView;
	}
	
	private String changeTextToEmotion(String text){
		text = text.replaceAll("\\:\\)", getImageTag(":)")).replaceAll("\\:\\(", getImageTag(":(")).replaceAll("8\\)", getImageTag("8)")).replaceAll("\\:P", getImageTag(":P")).replaceAll("\\:D", getImageTag(":D")).replaceAll("\\(happy\\)", getImageTag("(happy)")).replaceAll("\\:p", getImageTag(":P")).replaceAll("\\:d", getImageTag(":D")).replaceAll("\\(HAPPY\\)", getImageTag("(happy)"));
		
		return text;
	}
	
	private String getImageTag(String regExpr){
		String src = "";
		if(regExpr.equals(":)")){
			src = "smile.png";
		}
		if(regExpr.equals(":(")){
			src = "sad.png";
		}
		if(regExpr.equals(":P")){
			src = "toungle.png";
		}
		if(regExpr.equals(":D")){
			src = "laught.png";
		}
		if(regExpr.equals("8)")){
			src = "cool.png";
		}
		if(regExpr.equals("(happy)")){
			src = "happy.png";
		}
		
		if(!src.equals("")){
			return "<img src = '"+src+"' width='40' height='40'/>";
		}else{
			System.out.println("none -----------");
		}
		return regExpr;
	}

}

